var skillAttack = require("../../domain/skillAttack")
var Player = require("../../domain/player")
var playerCon = require('../../domain/playerCon')
var redisClient = require('../../dao/redis/redisClient')
var utils = require("../../util/utils")
var Bag = require("../../domain/bag")

module.exports = function(app) {
    app.post('/user/register', function(req, res) {
        var username = req.body.username
        var password = req.body.password
        var nowtime = new Date().getTime()
        var id = utils.uuid(7)
        redisClient.exists('u:' + username + ':id', function(err, ret) {
            if (err || ret !== 0) {
                res.send({
                    error: 2
                })
                return
            }
            var keyVals = []
            //id
            keyVals.push('u:' + username + ':id')
            keyVals.push(id)
            //名字
            keyVals.push('u:' + id + ':name')
            keyVals.push(username)
            //密码
            keyVals.push('u:' + id + ':pwd')
            keyVals.push(password)
            //登陆时间
            keyVals.push('u:' + id + ':login')
            keyVals.push(nowtime)
            //注册时间
            keyVals.push('u:' + id + ':reg')
            keyVals.push(nowtime)
            //登陆服务器id
            //keyVals.push('u:' + username + ':id')
            //keyVals.push(1)
            console.log(keyVals)
            redisClient.mset(keyVals, function(err, ret) {
                res.send({
                    error: 0
                })
            })
        })
    })

    app.post('/user/login', function(req, res) {
        var username = req.body.username
        var password = req.body.password
        redisClient.get('u:' + username + ':id', function(err, id) {
            if (err || !id) {
                res.send({
                    error: 2
                })
                return
            }
            redisClient.get('u:' + id + ':pwd', function(err, ret) {
                if (err || ret !== password) {
                    res.send({
                        error: 3
                    })
                    return
                }
                req.session.userid = id
                req.session.username = username
                redisClient.get('u:' + id + ':pid', function(err, ret) {
                    if (!err && ret) {
                        req.session.playerid = ret
                    }
                    res.send({
                        error: 0
                    })
                })
            })
        })
    })

    app.all('/user/*', function(req, res, next) {
        //用户是否登录
        if (req.session.userid) {
            next()
        } else {
            res.send({
                error: 101
            })
            return
        }
    })

    app.post('/user/addPlayer', function(req, res) {
      /*  var bag = new Bag({})
        bag.addItems([12000,12000,11000,13000,11000,14000,11])
        bag.reduceItem(12000, 1)
        console.log(bag)

         res.send({
                error: 0
            })
        return*/
        var name = req.body.name
        var id = req.session.userid
        playerCon.addPlayer(id, name, function(err, player) {
            if (err) {
                res.send({
                    error: err
                })
                return
            }
            req.session.playerid = player.id
            res.send({
                error: 0,
                player: player
            })
        })
    })
}